const pageBehavior = require('../../utils/pageBehavior');

Page({
  behaviors: [pageBehavior],

  data: {
    isZoomed: false,
    locations: [],
    scale: 1,
    translateX: 0,
    translateY: 0
  },

  // 触摸相关变量
  touchStartDistance: 0,
  touchStartScale: 1,
  lastTouchTime: 0,
  lastTouchX: 0,
  lastTouchY: 0,
  startTranslateX: 0,
  startTranslateY: 0,

  onLoad() {
    const locations = this.createLocations();
    this.setData({
      locations
    });
  },

  createLocations() {
    return [
      {
        id: 1,
        name: '迎新报到中心',
        category: '报到',
        room: '开放时间 08:30-18:00',
        address: '学术交流中心一层大厅',
        description: '新生报到、资料提交及校园卡领取处，抵校后请第一时间前往。',
        latitude: 31.215281,
        longitude: 121.437482,
        status: 'info'
      },
      {
        id: 2,
        name: '体检中心',
        category: '健康',
        room: '开放时间 09:00-17:00',
        address: '校医院二层体检区',
        description: '完成入学体检及疫苗补种确认，请携带身份证件。',
        latitude: 31.217652,
        longitude: 121.440312,
        status: 'success'
      },
      {
        id: 3,
        name: '绿色通道服务台',
        category: '资助',
        room: '开放时间 09:00-16:30',
        address: '行政楼一层大厅右侧',
        description: '提供学费缓缴、助学贷款、临时困难补助等服务咨询办理。',
        latitude: 31.214178,
        longitude: 121.434925,
        status: 'warning'
      }
    ];
  },

  // 点击图片，放大显示
  onImageTap() {
    this.setData({
      isZoomed: true,
      scale: 1,
      translateX: 0,
      translateY: 0
    });
  },

  // 点击遮罩层空白处，缩小图片
  onOverlayTap() {
    this.setData({
      isZoomed: false,
      scale: 1,
      translateX: 0,
      translateY: 0
    });
  },

  // 阻止点击图片时关闭遮罩层，并实现双击功能
  onZoomedImageTap(e) {
    const currentTime = Date.now();
    const timeDiff = currentTime - this.lastTouchTime;
    
    // 双击检测（300ms内连续点击两次）
    if (timeDiff < 300 && timeDiff > 0) {
      // 双击：放大到2倍或还原
      const newScale = this.data.scale === 1 ? 2 : 1;
      this.setData({
        scale: newScale,
        translateX: 0,
        translateY: 0
      });
    }
    
    this.lastTouchTime = currentTime;
  },

  // 阻止提示文字点击时关闭遮罩层
  preventClose() {
    // 什么都不做，只是阻止事件冒泡
  },

  // 触摸开始
  onTouchStart(e) {
    if (e.touches.length === 2) {
      // 双指触摸：记录初始距离
      const touch1 = e.touches[0];
      const touch2 = e.touches[1];
      this.touchStartDistance = this.getDistance(touch1, touch2);
      this.touchStartScale = this.data.scale;
    } else if (e.touches.length === 1) {
      // 单指触摸：记录初始位置
      this.lastTouchX = e.touches[0].pageX;
      this.lastTouchY = e.touches[0].pageY;
      this.startTranslateX = this.data.translateX;
      this.startTranslateY = this.data.translateY;
    }
  },

  // 触摸移动
  onTouchMove(e) {
    if (e.touches.length === 2) {
      // 双指缩放
      const touch1 = e.touches[0];
      const touch2 = e.touches[1];
      const currentDistance = this.getDistance(touch1, touch2);
      const scale = (currentDistance / this.touchStartDistance) * this.touchStartScale;
      
      // 限制缩放范围：0.5倍到4倍
      const finalScale = Math.max(0.5, Math.min(4, scale));
      
      this.setData({
        scale: finalScale
      });
    } else if (e.touches.length === 1) {
      // 单指拖动
      const deltaX = e.touches[0].pageX - this.lastTouchX;
      const deltaY = e.touches[0].pageY - this.lastTouchY;
      
      this.setData({
        translateX: this.startTranslateX + deltaX,
        translateY: this.startTranslateY + deltaY
      });
    }
  },

  // 触摸结束
  onTouchEnd(e) {
    // 重置触摸状态
    if (e.touches.length === 0) {
      this.touchStartDistance = 0;
    }
  },

  // 计算两点之间的距离
  getDistance(touch1, touch2) {
    const x = touch2.pageX - touch1.pageX;
    const y = touch2.pageY - touch1.pageY;
    return Math.sqrt(x * x + y * y);
  },

  onSelect(event) {
    // 可以保留这个方法，如果需要点击地点卡片时的交互
  }
});
